import {getSetting,chooseAddress,openSetting,showModal,showToast} from "../../utils/asyncWx"
// 语法转换的包
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    allChecked :false,
    totalPrice:0,
    totalNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onShow(){
    // 获取缓存中的地址
    const address = wx.getStorageSync("address");
    // 获取缓存中的购物车的数据
    const cart = wx.getStorageSync("cart")||[];
    // 计算全选
    this.setData({address});
    this.setCart(cart);
  },
  async handleChooseAddress(){
    try {
      // wx.getSetting({
      //   success:(result) => {
      //     //获取权限状态
      //     const scopeAddress = result.authSetting["scope.address"];
      //     if (scopeAddress === true||scopeAddress === undefined) {
      //       wx.chooseAddress({
      //         success:(result1) => {
      //             console.log(result1);
      //         }
      //     })
      //     }else{
      //       // 诱导用户打开授权页面 但是微信调用地址api现在不需要授权了
      //       wx.openSetting({
      //         success:(result2) => {
      //           wx.chooseAddress({
      //             success:(result3) => {
      //                 console.log(result3);
      //             }
      //         })
      //       }
      //       })
      //     }
      //   }
      // })
      // 获取权限状态
      const res1 = await getSetting()
      const scopeAddress = res1.authSetting["scope.address"]
      // 判断权限状态
      if (scopeAddress === false){
        // 诱导用户打开授权页面 但是微信调用地址api现在不需要授权了
        await openSetting();
      }
      // 调用获取地址的接口
      let address =  await chooseAddress();
      address.all = address.provinceName+address.cityName+address.countyName+address.detailInfo
      // 存入缓存
      wx.setStorageSync("address",address );
        
    }catch (error) {
      console.log(error);
    }
  } ,
  // 商品的选中
  handleItemChange(e){
    //获取被修改的商品ID
    const goods_id = e.currentTarget.dataset.id;
    // 获取购物车数组
    let {cart} = this.data;
    //找到被修改的对象
    let index = cart.findIndex(v=>v.goods_id===goods_id);
    // 选中状态取反
    cart[index].checked = !cart[index].checked;
    this.setCart(cart);
    
  },
  //设置购物车的状态 底部工具栏重新计算
  setCart(cart){
    let allChecked = true
      // 总价格 总数量
      let totalPrice = 0;
      let totalNum = 0;
      cart.forEach(v => {
        if (v.checked) {
          totalPrice += v.num * v.goods_price;
          totalNum += v.num;
        } else {
          allChecked = false;
        }
      });
      // 判断数组是否为空
      allChecked = cart.length!=0?allChecked:false;
      this.setData({
        cart,
        allChecked,
        totalPrice,
        totalNum
      })
      wx.setStorageSync("cart", cart);
  },
  //商品全选功能
  handleAllcheckChange(){
    let {cart,allChecked} = this.data;
    allChecked=!allChecked;
    cart.forEach(v=>v.checked=allChecked)
    this.setCart(cart)
  },
  // 获取商品数量的编辑功能
  async handleItemNumEdit(e){
    //获取传递参数
    const {operation,id} = e.currentTarget.dataset
    //获取购物车数组
    let {cart} = this.data
    //找到购物车索引
    const index = cart.findIndex(v=>v.goods_id==id)
    if (cart[index].num===1&&operation===-1) {
      const res = await showModal({content:"你是否要删除？"})
      if (res.confirm) {
        cart.splice(index,1)
        this.setCart(cart)
      }
    }else{
       //修改数量
    cart[index].num+=operation
    //设置回缓存和 data中
    this.setCart(cart)
    }
  },
  //点击支付功能
  async handlePay(){
    const {address,totalNum} = this.data
    if(!address.userName){
      await showToast({title:"您还没有选择收货地址"})
    }
    if(totalNum ===0){
      await showToast({title:"您还未选购商品"})
    }
    wx.navigateTo({
      url: '/pages/pay/index'
    })
  }
})